<?php

class Uuch_Reader_JdReader extends Uuch_Reader_ProductReader{
    
    public function getInfo($url) {
        $client = new Zend_Http_Client();
        $client->setEncType('');
        $client->setUri($url);
        $response = $client->request();
        $body = mb_convert_encoding($response->getBody(),'utf-8','gbk');
        
        $uri = parse_url($url);
        $name = explode('.',$uri['host']);
        $reader = ucfirst(strtolower($name[count($name)-2]));
        $config = Application_Model_ReaderConfig::getBySite($reader);
        
        //$titleReg = '#<h1>([\W\w.]+?)<#i';
        
        $product = new Application_Model_Product();
        $matches = array();
		if (preg_match('{<div id="name">\s*<h1>(.*?)</h1>\s*</div>}', $body, $matches)) {
			$product->Name = $matches[1];			
		} else if(preg_match($config->ProductNameRegex, $body,$matches)){
            $product->Name = $matches[1];
        }
        
        $id = $url;
        if(preg_match($config->SkuRegex, $url,$matches)){
            $id= $matches[1];
        }
        
        $html = file_get_contents('http://p.3.cn/prices/mgets?type=1&skuIds=J_'. $id);
        $prires = json_decode($html);
        $price = $prires[0]->p;
        
        $product->Price = $price;
        $product->Sku = $url;
        $product->Url = $url;
        
        $product->ShipFee = 0;
        $product->Quantity = 999;
        
        //$imageReg = '#<img onerror="this.src=\'http://www.360buy.com/images/none/none_347.gif\'"[\s]+?src="(.+?)" width#im';
        if(preg_match($config->ImageRegex, $body,$matches)){
            $product->Image = $matches[1];
        }
        
        $shopName = '京东';
        if($config->ShopRegex && preg_match($config->ShopRegex, $body,$matches)){
            $shopName = $matches[1];
        }
        
        $shop = Application_Model_Shop::getByName($shopName);
        if (NULL == $shop){
            $shop = new Application_Model_Shop();
            $shop->Name = $shopName;
            $shop->IsTaobao = True;
            $shop->Url = $uri['host'];
            $shop->save();
        }
        $product->ShopId = $shop->Id;
        
        $product->save();
        return $product;
    }
	
	public function getInfoForNew($url) {
        $client = new Zend_Http_Client();
        $client->setEncType('');
        $client->setUri($url);
        $response = $client->request();
        $body = mb_convert_encoding($response->getBody(),'utf-8','gbk');
        
        $uri = parse_url($url);
        $name = explode('.',$uri['host']);
        $reader = ucfirst(strtolower($name[count($name)-2]));
        $config = Application_Model_ReaderConfig::getBySite($reader);
        
        //$titleReg = '#<h1>([\W\w.]+?)<#i';
        
        $product = new Application_Model_Product();
        $matches = array();
		if (preg_match('/<div class="sku-name">\s[^>]*(.*?)<\/div>/is', $body, $matches)) {
            $product->Name = trim(trim($matches[1],'>'));           
        }else if(preg_match('/<div class="sku-name">\s*(.*?)<\/div>/is', $body,$matches)){
            $product->Name = trim($matches[1]);
        }else if(preg_match($config->ProductNameRegex, $body,$matches)){
            $product->Name = $matches[1];
        }
        
        $id = $url;
        if(preg_match($config->SkuRegex, $url,$matches)){
            $id= $matches[1];
        }
        
        $html = file_get_contents('http://p.3.cn/prices/mgets?type=1&skuIds=J_'. $id);
        $prires = json_decode($html);
        $price = $prires[0]->p;
        
        $product->Price = $price;		
        $product->Sku = $url;
        $product->Url = $url;
        
        $product->ShipFee = 0;
        $product->Quantity = 999;
        
        //$imageReg = '#<img onerror="this.src=\'http://www.360buy.com/images/none/none_347.gif\'"[\s]+?src="(.+?)" width#im';
         $ImageRegex="/<img id=\"spec-img\" width=\"350\"[\s]+?data-origin=\"(.+?)\"/i";
        if(preg_match($ImageRegex, $body,$matches)){
            $product->Image = $matches[1];
        }
        
        $shopName = '京东';
        if($config->ShopRegex && preg_match($config->ShopRegex, $body,$matches)){
            $str = strip_tags(trim($matches[1]));
            $name = strpos($str,"\n");
            $shopName = substr($str,0,$name);
        }
		
		if (!$product->Price) {
			return NULL;
		}
        
        $shop = Application_Model_Shop::getByName($shopName);
        if (NULL == $shop){
            $shop = new Application_Model_Shop();
            $shop->Name = $shopName;
            $shop->IsTaobao = True;
            $shop->Url = $uri['host'];
            //$shop->save();
        }
        //$product->ShopId = $shop->Id;
        
        //$product->save();
        //return $product;
		return array(
			'product' => $product, 
			'shopName' => $shopName,
			'siteName' => '京东',
			'shopUrl' => $url,
			'url' => $url,
			'siteUrl' => $uri['scheme'] . '://' . $uri['host']);
    }
    
}